/*     */ package com.zimbra.cs.service.account;
/*     */ 
/*     */ import com.google.common.collect.ImmutableSet;
/*     */ import com.google.common.collect.Lists;
/*     */ import com.sun.mail.smtp.SMTPMessage;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.mime.shim.JavaMailInternetAddress;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AccountConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.L10nUtil;
/*     */ import com.zimbra.common.util.L10nUtil.MsgKey;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.common.zmime.ZMimeBodyPart;
/*     */ import com.zimbra.common.zmime.ZMimeMultipart;
/*     */ import com.zimbra.cs.account.AccessManager;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Group;
/*     */ import com.zimbra.cs.account.Group.GroupOwner;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.accesscontrol.ACLUtil;
/*     */ import com.zimbra.cs.account.accesscontrol.GranteeType;
/*     */ import com.zimbra.cs.account.accesscontrol.Right;
/*     */ import com.zimbra.cs.account.accesscontrol.RightCommand;
/*     */ import com.zimbra.cs.account.accesscontrol.RightManager;
/*     */ import com.zimbra.cs.account.accesscontrol.TargetType;
/*     */ import com.zimbra.cs.account.accesscontrol.ZimbraACE;
/*     */ import com.zimbra.cs.account.accesscontrol.ZimbraACE.ExternalGroupInfo;
/*     */ import com.zimbra.cs.account.names.NameUtil.EmailAddress;
/*     */ import com.zimbra.cs.util.AccountUtil;
/*     */ import com.zimbra.cs.util.JMSession;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.account.type.DistributionListAction.Operation;
/*     */ import com.zimbra.soap.account.type.DistributionListSubscribeOp;
/*     */ import com.zimbra.soap.admin.type.GranteeSelector.GranteeBy;
/*     */ import com.zimbra.soap.type.TargetBy;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.Collections;
/*     */ import java.util.Date;
/*     */ import java.util.LinkedHashMap;
/*     */ import java.util.LinkedList;
/*     */ import java.util.List;
/*     */ import java.util.Locale;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ import javax.activation.DataHandler;
/*     */ import javax.mail.Address;
/*     */ import javax.mail.Message.RecipientType;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.MimeBodyPart;
/*     */ import javax.mail.internet.MimeMultipart;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DistributionListAction
/*     */   extends DistributionListDocumentHandler
/*     */ {
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  78 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  79 */     Provisioning prov = Provisioning.getInstance();
/*  80 */     Account acct = getAuthenticatedAccount(zsc);
/*     */     
/*  82 */     Group group = getGroupBasic(request, prov);
/*  83 */     DistributionListActionHandler handler = new DistributionListActionHandler(group, request, prov, acct);
/*     */     
/*  85 */     handler.handle();
/*     */     
/*  87 */     Element response = zsc.createElement(AccountConstants.DISTRIBUTION_LIST_ACTION_RESPONSE);
/*  88 */     return response;
/*     */   }
/*     */   
/*     */   private static class DistributionListActionHandler extends DistributionListDocumentHandler.SynchronizedGroupHandler
/*     */   {
/*     */     private final Element request;
/*     */     private final Provisioning prov;
/*     */     private final Account acct;
/*     */     
/*     */     protected DistributionListActionHandler(Group group, Element request, Provisioning prov, Account acct) {
/*  98 */       super();
/*  99 */       this.request = request;
/* 100 */       this.prov = prov;
/* 101 */       this.acct = acct;
/*     */     }
/*     */     
/*     */     protected void handleRequest() throws ServiceException
/*     */     {
/* 106 */       Element eAction = this.request.getElement("action");
/* 107 */       DistributionListAction.Operation op = DistributionListAction.Operation.fromString(eAction.getAttribute("op"));
/*     */       
/*     */ 
/*     */ 
/* 111 */       if (!Group.GroupOwner.hasOwnerPrivilege(this.acct, this.group)) {
/* 112 */         throw ServiceException.PERM_DENIED("you do not have sufficient rights to access this distribution list");
/*     */       }
/*     */       
/*     */ 
/* 116 */       DistributionListAction.DLActionHandler handler = null;
/* 117 */       switch (DistributionListAction.1.$SwitchMap$com$zimbra$soap$account$type$DistributionListAction$Operation[op.ordinal()]) {
/*     */       case 1: 
/* 119 */         handler = new DistributionListAction.DeleteHandler(eAction, this.group, this.prov, this.acct);
/* 120 */         break;
/*     */       case 2: 
/* 122 */         handler = new DistributionListAction.ModifyHandler(eAction, this.group, this.prov, this.acct);
/* 123 */         break;
/*     */       case 3: 
/* 125 */         handler = new DistributionListAction.RenameHandler(eAction, this.group, this.prov, this.acct);
/* 126 */         break;
/*     */       case 4: 
/* 128 */         handler = new DistributionListAction.AddOwnersHandler(eAction, this.group, this.prov, this.acct);
/* 129 */         break;
/*     */       case 5: 
/* 131 */         handler = new DistributionListAction.RemoveOwnersHandler(eAction, this.group, this.prov, this.acct);
/* 132 */         break;
/*     */       case 6: 
/* 134 */         handler = new DistributionListAction.SetOwnersHandler(eAction, this.group, this.prov, this.acct);
/* 135 */         break;
/*     */       case 7: 
/* 137 */         handler = new DistributionListAction.GrantRightsHandler(eAction, this.group, this.prov, this.acct);
/* 138 */         break;
/*     */       case 8: 
/* 140 */         handler = new DistributionListAction.RevokeRightsHandler(eAction, this.group, this.prov, this.acct);
/* 141 */         break;
/*     */       case 9: 
/* 143 */         handler = new DistributionListAction.SetRightsHandler(eAction, this.group, this.prov, this.acct);
/* 144 */         break;
/*     */       case 10: 
/* 146 */         handler = new DistributionListAction.AddMembersHandler(eAction, this.group, this.prov, this.acct);
/* 147 */         break;
/*     */       case 11: 
/* 149 */         handler = new DistributionListAction.RemoveMembersHandler(eAction, this.group, this.prov, this.acct);
/* 150 */         break;
/*     */       case 12: 
/* 152 */         handler = new DistributionListAction.AcceptSubsReqHandler(eAction, this.group, this.prov, this.acct);
/* 153 */         break;
/*     */       case 13: 
/* 155 */         handler = new DistributionListAction.RejectSubsReqHandler(eAction, this.group, this.prov, this.acct);
/* 156 */         break;
/*     */       default: 
/* 158 */         throw ServiceException.FAILURE("unsupported op:" + op.name(), null);
/*     */       }
/*     */       
/* 161 */       handler.handle();
/*     */     }
/*     */   }
/*     */   
/*     */   private static abstract class DLActionHandler
/*     */   {
/*     */     protected Element eAction;
/*     */     protected Group group;
/*     */     protected Provisioning prov;
/*     */     protected Account authedAcct;
/*     */     
/*     */     protected DLActionHandler(Element request, Group group, Provisioning prov, Account authedAcct)
/*     */     {
/* 174 */       this.eAction = request;
/* 175 */       this.group = group;
/* 176 */       this.prov = prov;
/* 177 */       this.authedAcct = authedAcct;
/*     */     }
/*     */     
/*     */     abstract void handle() throws ServiceException;
/*     */     
/*     */     abstract DistributionListAction.Operation getAction();
/*     */   }
/*     */   
/*     */   private static class DeleteHandler extends DistributionListAction.DLActionHandler
/*     */   {
/*     */     protected DeleteHandler(Element eAction, Group group, Provisioning prov, Account authedAcct) {
/* 188 */       super(group, prov, authedAcct);
/*     */     }
/*     */     
/*     */     DistributionListAction.Operation getAction()
/*     */     {
/* 193 */       return DistributionListAction.Operation.delete;
/*     */     }
/*     */     
/*     */     void handle() throws ServiceException
/*     */     {
/* 198 */       if (!AccessManager.getInstance().canCreateGroup(this.authedAcct, this.group.getName())) {
/* 199 */         throw ServiceException.PERM_DENIED("you do not have sufficient rights to delete this distribution list");
/*     */       }
/*     */       
/*     */ 
/* 203 */       this.prov.deleteGroup(this.group.getId());
/*     */       
/* 205 */       ZimbraLog.security.info(ZimbraLog.encodeAttrs(new String[] { "cmd", "DistributionListAction", "op", getAction().name(), "name", this.group.getName(), "id", this.group.getId() }));
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private static class ModifyHandler
/*     */     extends DistributionListAction.DLActionHandler
/*     */   {
/*     */     protected ModifyHandler(Element eAction, Group group, Provisioning prov, Account authedAcct)
/*     */     {
/* 216 */       super(group, prov, authedAcct);
/*     */     }
/*     */     
/*     */     DistributionListAction.Operation getAction()
/*     */     {
/* 221 */       return DistributionListAction.Operation.modify;
/*     */     }
/*     */     
/*     */     void handle() throws ServiceException
/*     */     {
/* 226 */       Map<String, Object> attrs = AccountService.getKeyValuePairs(this.eAction, "a", "n");
/*     */       
/* 228 */       this.prov.modifyAttrs(this.group, attrs, true);
/*     */       
/* 230 */       ZimbraLog.security.info(ZimbraLog.encodeAttrs(new String[] { "cmd", "DistributionListAction", "op", getAction().name(), "name", this.group.getName() }, attrs));
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   private static class RenameHandler
/*     */     extends DistributionListAction.DLActionHandler
/*     */   {
/*     */     protected RenameHandler(Element eAction, Group group, Provisioning prov, Account authedAcct)
/*     */     {
/* 240 */       super(group, prov, authedAcct);
/*     */     }
/*     */     
/*     */     DistributionListAction.Operation getAction()
/*     */     {
/* 245 */       return DistributionListAction.Operation.rename;
/*     */     }
/*     */     
/*     */     void handle()
/*     */       throws ServiceException
/*     */     {
/* 251 */       Element eNewName = this.eAction.getElement("newName");
/* 252 */       String newName = eNewName.getText();
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 259 */       String oldName = this.group.getName();
/* 260 */       if (!AccessManager.getInstance().canCreateGroup(this.authedAcct, oldName)) {
/* 261 */         throw ServiceException.PERM_DENIED("you do not have sufficient rights to rename this distribution list");
/*     */       }
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 268 */       String curDomain = new NameUtil.EmailAddress(oldName).getDomain();
/* 269 */       String newDomain = new NameUtil.EmailAddress(newName).getDomain();
/* 270 */       if ((!curDomain.equalsIgnoreCase(newDomain)) && 
/* 271 */         (!AccessManager.getInstance().canCreateGroup(this.authedAcct, newName))) {
/* 272 */         throw ServiceException.PERM_DENIED("you do not have sufficient rights to rename this distribution list");
/*     */       }
/*     */       
/*     */ 
/*     */ 
/* 277 */       this.prov.renameGroup(this.group.getId(), newName);
/*     */       
/* 279 */       ZimbraLog.security.info(ZimbraLog.encodeAttrs(new String[] { "cmd", "DistributionListAction", "op", getAction().name(), "name", oldName, "newName", newName }));
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   private static abstract class ModifyRightHandler
/*     */     extends DistributionListAction.DLActionHandler
/*     */   {
/*     */     protected ModifyRightHandler(Element eAction, Group group, Provisioning prov, Account authedAcct)
/*     */     {
/* 289 */       super(group, prov, authedAcct);
/*     */     }
/*     */     
/*     */     protected class Grantee {
/*     */       GranteeType type;
/*     */       GranteeSelector.GranteeBy by;
/*     */       String grantee;
/*     */       
/*     */       private Grantee(GranteeType type, GranteeSelector.GranteeBy by, String grantee) {
/* 298 */         this.type = type;
/* 299 */         this.by = by;
/* 300 */         this.grantee = grantee;
/*     */       }
/*     */     }
/*     */     
/*     */     protected List<Grantee> parseGrantees(Element parent, String granteeElem) throws ServiceException
/*     */     {
/* 306 */       List<Grantee> grantees = Lists.newArrayList();
/*     */       
/* 308 */       String domain = this.prov.getDomain(this.authedAcct).getName();
/* 309 */       for (Element eGrantee : parent.listElements(granteeElem)) {
/* 310 */         GranteeType type = GranteeType.fromCode(eGrantee.getAttribute("type"));
/*     */         
/* 312 */         GranteeSelector.GranteeBy by = null;
/* 313 */         String grantee = null;
/*     */         
/* 315 */         if (type.needsGranteeIdentity()) {
/* 316 */           by = GranteeSelector.GranteeBy.fromString(eGrantee.getAttribute("by"));
/* 317 */           grantee = eGrantee.getText();
/*     */         }
/*     */         
/* 320 */         type = GranteeType.determineGranteeType(type, by, grantee, domain);
/*     */         
/* 322 */         if (type == GranteeType.GT_EXT_GROUP) {
/* 323 */           grantee = ZimbraACE.ExternalGroupInfo.encodeIfExtGroupNameMissingDomain(domain, grantee);
/*     */         }
/* 325 */         grantees.add(new Grantee(type, by, grantee, null));
/*     */       }
/*     */       
/* 328 */       return grantees;
/*     */     }
/*     */     
/*     */     protected void verifyGrantRight(Right right, GranteeType granteeType, GranteeSelector.GranteeBy granteeBy, String grantee) throws ServiceException
/*     */     {
/* 333 */       RightCommand.verifyGrantRight(this.prov, null, TargetType.dl.getCode(), TargetBy.id, this.group.getId(), granteeType.getCode(), granteeBy, grantee, null, right.getName(), null);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     protected void grantRight(Right right, GranteeType granteeType, GranteeSelector.GranteeBy granteeBy, String grantee)
/*     */       throws ServiceException
/*     */     {
/* 343 */       RightCommand.grantRight(this.prov, null, TargetType.dl.getCode(), TargetBy.id, this.group.getId(), granteeType.getCode(), granteeBy, grantee, null, right.getName(), null);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 349 */       ZimbraLog.security.info(ZimbraLog.encodeAttrs(new String[] { "cmd", "DistributionListAction", "op", getAction().name(), "name", this.group.getName(), "type", granteeType.getCode(), "grantee", grantee }));
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */     protected void revokeRight(Right right, GranteeType granteeType, GranteeSelector.GranteeBy granteeBy, String grantee)
/*     */       throws ServiceException
/*     */     {
/* 358 */       RightCommand.revokeRight(this.prov, null, TargetType.dl.getCode(), TargetBy.id, this.group.getId(), granteeType.getCode(), granteeBy, grantee, right.getName(), null);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 364 */       ZimbraLog.security.info(ZimbraLog.encodeAttrs(new String[] { "cmd", "DistributionListAction", "op", getAction().name(), "name", this.group.getName(), "type", granteeType.getCode(), "grantee", grantee }));
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static class AddOwnersHandler
/*     */     extends DistributionListAction.ModifyRightHandler
/*     */   {
/* 377 */     private static final Set<GranteeType> VALID_GRANTEE_TYPES = ImmutableSet.of(GranteeType.GT_USER, GranteeType.GT_GROUP, GranteeType.GT_EXT_GROUP, GranteeType.GT_DOMAIN, GranteeType.GT_AUTHUSER);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     protected AddOwnersHandler(Element eAction, Group group, Provisioning prov, Account authedAcct)
/*     */     {
/* 386 */       super(group, prov, authedAcct);
/*     */     }
/*     */     
/*     */     DistributionListAction.Operation getAction()
/*     */     {
/* 391 */       return DistributionListAction.Operation.addOwners;
/*     */     }
/*     */     
/*     */     void handle() throws ServiceException
/*     */     {
/* 396 */       List<DistributionListAction.ModifyRightHandler.Grantee> owners = parseGrantees(this.eAction, "owner");
/*     */       
/* 398 */       for (DistributionListAction.ModifyRightHandler.Grantee owner : owners) {
/* 399 */         verifyOwner(this, owner.type, owner.by, owner.grantee);
/*     */       }
/*     */       
/* 402 */       for (DistributionListAction.ModifyRightHandler.Grantee owner : owners) {
/* 403 */         addOwner(this, owner.type, owner.by, owner.grantee);
/*     */       }
/*     */     }
/*     */     
/*     */     private static void verifyOwner(DistributionListAction.ModifyRightHandler handler, GranteeType granteeType, GranteeSelector.GranteeBy granteeBy, String grantee)
/*     */       throws ServiceException
/*     */     {
/* 410 */       if (!VALID_GRANTEE_TYPES.contains(granteeType)) {
/* 411 */         throw ServiceException.INVALID_REQUEST("invalid grantee type for groups owner", null);
/*     */       }
/*     */       
/*     */ 
/* 415 */       handler.verifyGrantRight(Group.GroupOwner.GROUP_OWNER_RIGHT, granteeType, granteeBy, grantee);
/*     */     }
/*     */     
/*     */ 
/*     */     private static void addOwner(DistributionListAction.ModifyRightHandler handler, GranteeType granteeType, GranteeSelector.GranteeBy granteeBy, String grantee)
/*     */       throws ServiceException
/*     */     {
/* 422 */       handler.grantRight(Group.GroupOwner.GROUP_OWNER_RIGHT, granteeType, granteeBy, grantee);
/*     */     }
/*     */   }
/*     */   
/*     */   static class RemoveOwnersHandler
/*     */     extends DistributionListAction.ModifyRightHandler
/*     */   {
/*     */     protected RemoveOwnersHandler(Element eAction, Group group, Provisioning prov, Account authedAcct)
/*     */     {
/* 431 */       super(group, prov, authedAcct);
/*     */     }
/*     */     
/*     */     DistributionListAction.Operation getAction()
/*     */     {
/* 436 */       return DistributionListAction.Operation.removeOwners;
/*     */     }
/*     */     
/*     */     void handle() throws ServiceException
/*     */     {
/* 441 */       List<DistributionListAction.ModifyRightHandler.Grantee> owners = parseGrantees(this.eAction, "owner");
/* 442 */       for (DistributionListAction.ModifyRightHandler.Grantee owner : owners) {
/* 443 */         removeOwner(this, owner.type, owner.by, owner.grantee);
/*     */       }
/*     */     }
/*     */     
/*     */     private static void removeOwner(DistributionListAction.ModifyRightHandler handler, GranteeType granteeType, GranteeSelector.GranteeBy granteeBy, String grantee) throws ServiceException
/*     */     {
/* 449 */       handler.revokeRight(Group.GroupOwner.GROUP_OWNER_RIGHT, granteeType, granteeBy, grantee);
/*     */     }
/*     */   }
/*     */   
/*     */   static class SetOwnersHandler
/*     */     extends DistributionListAction.ModifyRightHandler
/*     */   {
/*     */     protected SetOwnersHandler(Element eAction, Group group, Provisioning prov, Account authedAcct)
/*     */     {
/* 458 */       super(group, prov, authedAcct);
/*     */     }
/*     */     
/*     */     DistributionListAction.Operation getAction()
/*     */     {
/* 463 */       return DistributionListAction.Operation.setOwners;
/*     */     }
/*     */     
/*     */     void handle() throws ServiceException
/*     */     {
/* 468 */       List<DistributionListAction.ModifyRightHandler.Grantee> owners = parseGrantees(this.eAction, "owner");
/*     */       
/*     */ 
/*     */ 
/* 472 */       for (DistributionListAction.ModifyRightHandler.Grantee owner : owners) {
/* 473 */         DistributionListAction.AddOwnersHandler.access$100(this, owner.type, owner.by, owner.grantee);
/*     */       }
/*     */       
/*     */ 
/* 477 */       List<Group.GroupOwner> curOwners = Group.GroupOwner.getOwners(this.group, false);
/* 478 */       for (Group.GroupOwner owner : curOwners) {
/* 479 */         DistributionListAction.RemoveOwnersHandler.access$200(this, owner.getType(), GranteeSelector.GranteeBy.id, owner.getId());
/*     */       }
/*     */       
/*     */ 
/*     */ 
/* 484 */       for (DistributionListAction.ModifyRightHandler.Grantee owner : owners) {
/* 485 */         DistributionListAction.AddOwnersHandler.access$300(this, owner.type, owner.by, owner.grantee);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private static abstract class ModifyMultipleRightsHandler extends DistributionListAction.ModifyRightHandler
/*     */   {
/*     */     protected ModifyMultipleRightsHandler(Element eAction, Group group, Provisioning prov, Account authedAcct) {
/* 493 */       super(group, prov, authedAcct);
/*     */     }
/*     */     
/*     */     protected Map<Right, List<DistributionListAction.ModifyRightHandler.Grantee>> parseRights() throws ServiceException {
/* 497 */       RightManager rightMgr = RightManager.getInstance();
/*     */       
/*     */ 
/* 500 */       Map<Right, List<DistributionListAction.ModifyRightHandler.Grantee>> rights = new LinkedHashMap();
/* 501 */       for (Element eRight : this.eAction.listElements("right")) {
/* 502 */         Right right = rightMgr.getUserRight(eRight.getAttribute("right"));
/*     */         
/* 504 */         if (Group.GroupOwner.GROUP_OWNER_RIGHT == right) {
/* 505 */           throw ServiceException.INVALID_REQUEST(right.getName() + " cannot be granted directly, use addOwners/removeOwners/setOwners" + " operation instead", null);
/*     */         }
/*     */         
/*     */ 
/* 509 */         List<DistributionListAction.ModifyRightHandler.Grantee> grantees = parseGrantees(eRight, "grantee");
/* 510 */         rights.put(right, grantees);
/*     */       }
/*     */       
/* 513 */       return rights;
/*     */     }
/*     */   }
/*     */   
/*     */   static class GrantRightsHandler extends DistributionListAction.ModifyMultipleRightsHandler
/*     */   {
/*     */     protected GrantRightsHandler(Element eAction, Group group, Provisioning prov, Account authedAcct) {
/* 520 */       super(group, prov, authedAcct);
/*     */     }
/*     */     
/*     */     DistributionListAction.Operation getAction()
/*     */     {
/* 525 */       return DistributionListAction.Operation.grantRights;
/*     */     }
/*     */     
/*     */     void handle() throws ServiceException
/*     */     {
/* 530 */       Map<Right, List<DistributionListAction.ModifyRightHandler.Grantee>> rights = parseRights();
/* 531 */       for (Map.Entry<Right, List<DistributionListAction.ModifyRightHandler.Grantee>> entry : rights.entrySet()) {
/* 532 */         right = (Right)entry.getKey();
/* 533 */         List<DistributionListAction.ModifyRightHandler.Grantee> grantees = (List)entry.getValue();
/* 534 */         for (DistributionListAction.ModifyRightHandler.Grantee grantee : grantees) {
/* 535 */           grantRight(right, grantee.type, grantee.by, grantee.grantee);
/*     */         }
/*     */       }
/*     */       Right right;
/*     */     }
/*     */   }
/*     */   
/*     */   static class RevokeRightsHandler extends DistributionListAction.ModifyMultipleRightsHandler {
/*     */     protected RevokeRightsHandler(Element eAction, Group group, Provisioning prov, Account authedAcct) {
/* 544 */       super(group, prov, authedAcct);
/*     */     }
/*     */     
/*     */     DistributionListAction.Operation getAction()
/*     */     {
/* 549 */       return DistributionListAction.Operation.revokeRights;
/*     */     }
/*     */     
/*     */     void handle() throws ServiceException
/*     */     {
/* 554 */       Map<Right, List<DistributionListAction.ModifyRightHandler.Grantee>> rights = parseRights();
/*     */       
/* 556 */       for (Map.Entry<Right, List<DistributionListAction.ModifyRightHandler.Grantee>> entry : rights.entrySet()) {
/* 557 */         right = (Right)entry.getKey();
/* 558 */         List<DistributionListAction.ModifyRightHandler.Grantee> grantees = (List)entry.getValue();
/* 559 */         for (DistributionListAction.ModifyRightHandler.Grantee grantee : grantees) {
/* 560 */           revokeRight(right, grantee.type, grantee.by, grantee.grantee);
/*     */         }
/*     */       }
/*     */       Right right;
/*     */     }
/*     */   }
/*     */   
/*     */   static class SetRightsHandler extends DistributionListAction.ModifyMultipleRightsHandler
/*     */   {
/*     */     protected SetRightsHandler(Element eAction, Group group, Provisioning prov, Account authedAcct) {
/* 570 */       super(group, prov, authedAcct);
/*     */     }
/*     */     
/*     */     DistributionListAction.Operation getAction()
/*     */     {
/* 575 */       return DistributionListAction.Operation.setRights;
/*     */     }
/*     */     
/*     */     void handle() throws ServiceException
/*     */     {
/* 580 */       Map<Right, List<DistributionListAction.ModifyRightHandler.Grantee>> rights = parseRights();
/*     */       
/* 582 */       for (Map.Entry<Right, List<DistributionListAction.ModifyRightHandler.Grantee>> entry : rights.entrySet()) {
/* 583 */         right = (Right)entry.getKey();
/* 584 */         List<DistributionListAction.ModifyRightHandler.Grantee> grantees = (List)entry.getValue();
/*     */         
/*     */ 
/* 587 */         List<ZimbraACE> acl = ACLUtil.getACEs(this.group, Collections.singleton(right));
/* 588 */         if (acl != null) {
/* 589 */           for (ZimbraACE ace : acl) {
/* 590 */             revokeRight(right, ace.getGranteeType(), GranteeSelector.GranteeBy.id, ace.getGrantee());
/*     */           }
/*     */         }
/*     */         
/*     */ 
/*     */ 
/* 596 */         for (DistributionListAction.ModifyRightHandler.Grantee grantee : grantees) {
/* 597 */           grantRight(right, grantee.type, grantee.by, grantee.grantee);
/*     */         }
/*     */       }
/*     */       Right right;
/*     */     }
/*     */   }
/*     */   
/*     */   private static class AddMembersHandler extends DistributionListAction.DLActionHandler
/*     */   {
/*     */     protected AddMembersHandler(Element eAction, Group group, Provisioning prov, Account authedAcct) {
/* 607 */       super(group, prov, authedAcct);
/*     */     }
/*     */     
/*     */     DistributionListAction.Operation getAction()
/*     */     {
/* 612 */       return DistributionListAction.Operation.addMembers;
/*     */     }
/*     */     
/*     */     void handle() throws ServiceException
/*     */     {
/* 617 */       List<String> memberList = new LinkedList();
/* 618 */       for (Element elem : this.eAction.listElements("dlm")) {
/* 619 */         memberList.add(elem.getTextTrim());
/*     */       }
/*     */       
/* 622 */       String[] members = (String[])memberList.toArray(new String[memberList.size()]);
/* 623 */       DistributionListDocumentHandler.addGroupMembers(this.prov, this.group, members);
/*     */       
/* 625 */       ZimbraLog.security.info(ZimbraLog.encodeAttrs(new String[] { "cmd", "DistributionListAction", "op", getAction().name(), "name", this.group.getName(), "members", Arrays.deepToString(members) }));
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   private static class RemoveMembersHandler
/*     */     extends DistributionListAction.DLActionHandler
/*     */   {
/*     */     protected RemoveMembersHandler(Element eAction, Group group, Provisioning prov, Account authedAcct)
/*     */     {
/* 635 */       super(group, prov, authedAcct);
/*     */     }
/*     */     
/*     */     DistributionListAction.Operation getAction()
/*     */     {
/* 640 */       return DistributionListAction.Operation.removeMembers;
/*     */     }
/*     */     
/*     */     void handle() throws ServiceException
/*     */     {
/* 645 */       List<String> memberList = new LinkedList();
/* 646 */       for (Element elem : this.eAction.listElements("dlm")) {
/* 647 */         memberList.add(elem.getTextTrim());
/*     */       }
/*     */       
/* 650 */       String[] members = (String[])memberList.toArray(new String[memberList.size()]);
/* 651 */       DistributionListDocumentHandler.removeGroupMembers(this.prov, this.group, members);
/*     */       
/* 653 */       ZimbraLog.security.info(ZimbraLog.encodeAttrs(new String[] { "cmd", "DistributionListAction", "op", getAction().name(), "name", this.group.getName(), "members", Arrays.deepToString(members) }));
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   private static class SubscriptionResponseSender
/*     */     extends DistributionListDocumentHandler.NotificationSender
/*     */   {
/*     */     private final Account ownerAcct;
/*     */     private final boolean bccOwners;
/*     */     private final boolean accepted;
/*     */     
/*     */     private SubscriptionResponseSender(Provisioning prov, Group group, Account ownerAcct, Account requestingAcct, DistributionListSubscribeOp op, boolean bccOwners, boolean accepted)
/*     */     {
/* 667 */       super(group, requestingAcct, op);
/* 668 */       this.ownerAcct = ownerAcct;
/* 669 */       this.bccOwners = bccOwners;
/* 670 */       this.accepted = accepted;
/*     */     }
/*     */     
/*     */     private void sendMessage() throws ServiceException {
/*     */       try {
/* 675 */         SMTPMessage out = new SMTPMessage(JMSession.getSmtpSession());
/*     */         
/* 677 */         Address fromAddr = AccountUtil.getFriendlyEmailAddress(this.ownerAcct);
/*     */         
/* 679 */         Address replyToAddr = fromAddr;
/* 680 */         String replyTo = this.ownerAcct.getAttr("zimbraPrefReplyToAddress");
/* 681 */         if (replyTo != null) {
/* 682 */           replyToAddr = new JavaMailInternetAddress(replyTo);
/*     */         }
/*     */         
/*     */ 
/* 686 */         out.setFrom(fromAddr);
/*     */         
/*     */ 
/* 689 */         out.setReplyTo(new Address[] { replyToAddr });
/*     */         
/*     */ 
/* 692 */         out.setRecipient(Message.RecipientType.TO, new JavaMailInternetAddress(this.requestingAcct.getName()));
/*     */         
/*     */ 
/*     */ 
/* 696 */         if (this.bccOwners) {
/* 697 */           List<String> owners = new ArrayList();
/* 698 */           Group.GroupOwner.getOwnerEmails(this.group, owners);
/*     */           
/* 700 */           List<Address> addrs = Lists.newArrayList();
/* 701 */           for (String ownerEmail : owners) {
/* 702 */             if (!ownerEmail.equals(this.ownerAcct.getName())) {
/* 703 */               addrs.add(new JavaMailInternetAddress(ownerEmail));
/*     */             }
/*     */           }
/* 706 */           if (!addrs.isEmpty()) {
/* 707 */             out.addRecipients(Message.RecipientType.BCC, (Address[])addrs.toArray(new Address[addrs.size()]));
/*     */           }
/*     */         }
/*     */         
/*     */ 
/*     */ 
/* 713 */         out.setSentDate(new Date());
/*     */         
/*     */ 
/* 716 */         Locale locale = getLocale(this.requestingAcct);
/*     */         
/*     */ 
/* 719 */         String subject = L10nUtil.getMessage(L10nUtil.MsgKey.dlSubscriptionResponseSubject, locale, new Object[0]);
/* 720 */         out.setSubject(subject);
/*     */         
/* 722 */         buildContentAndSend(out, locale, "group subscription response");
/*     */       }
/*     */       catch (MessagingException e) {
/* 725 */         ZimbraLog.account.warn("send share info notification failed, rcpt='" + this.requestingAcct.getName() + "'", e);
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */     protected MimeMultipart buildMailContent(Locale locale)
/*     */       throws MessagingException
/*     */     {
/* 734 */       String text = textPart(locale);
/* 735 */       String html = htmlPart(locale);
/*     */       
/*     */ 
/* 738 */       MimeMultipart mmp = new ZMimeMultipart("alternative");
/*     */       
/*     */ 
/* 741 */       MimeBodyPart textPart = new ZMimeBodyPart();
/* 742 */       textPart.setText(text, "utf-8");
/* 743 */       mmp.addBodyPart(textPart);
/*     */       
/*     */ 
/* 746 */       MimeBodyPart htmlPart = new ZMimeBodyPart();
/* 747 */       htmlPart.setDataHandler(new DataHandler(new DistributionListDocumentHandler.NotificationSender.HtmlPartDataSource(html)));
/* 748 */       mmp.addBodyPart(htmlPart);
/*     */       
/* 750 */       return mmp;
/*     */     }
/*     */     
/*     */     private String textPart(Locale locale) {
/* 754 */       StringBuilder sb = new StringBuilder();
/*     */       
/*     */       L10nUtil.MsgKey msgKey;
/*     */       L10nUtil.MsgKey msgKey;
/* 758 */       if (this.accepted) {
/* 759 */         msgKey = DistributionListSubscribeOp.subscribe == this.op ? L10nUtil.MsgKey.dlSubscribeResponseAcceptedText : L10nUtil.MsgKey.dlUnsubscribeResponseAcceptedText;
/*     */       }
/*     */       else
/*     */       {
/* 763 */         msgKey = DistributionListSubscribeOp.subscribe == this.op ? L10nUtil.MsgKey.dlSubscribeResponseRejectedText : L10nUtil.MsgKey.dlUnsubscribeResponseRejectedText;
/*     */       }
/*     */       
/*     */ 
/*     */ 
/* 768 */       sb.append("\n");
/* 769 */       sb.append(L10nUtil.getMessage(msgKey, locale, new Object[] { this.requestingAcct.getName(), this.group.getName() }));
/*     */       
/* 771 */       sb.append("\n\n");
/* 772 */       return sb.toString();
/*     */     }
/*     */     
/*     */     private String htmlPart(Locale locale) {
/* 776 */       StringBuilder sb = new StringBuilder();
/*     */       
/* 778 */       sb.append("<h4>\n");
/* 779 */       sb.append("<p>" + textPart(locale) + "</p>\n");
/* 780 */       sb.append("</h4>\n");
/* 781 */       sb.append("\n");
/* 782 */       return sb.toString();
/*     */     }
/*     */   }
/*     */   
/*     */   private static class AcceptSubsReqHandler
/*     */     extends DistributionListAction.DLActionHandler
/*     */   {
/*     */     protected AcceptSubsReqHandler(Element eAction, Group group, Provisioning prov, Account authedAcct)
/*     */     {
/* 791 */       super(group, prov, authedAcct);
/*     */     }
/*     */     
/*     */     DistributionListAction.Operation getAction()
/*     */     {
/* 796 */       return DistributionListAction.Operation.acceptSubsReq;
/*     */     }
/*     */     
/*     */     void handle()
/*     */       throws ServiceException
/*     */     {
/* 802 */       Element eSubsReq = this.eAction.getElement("subsReq");
/* 803 */       DistributionListSubscribeOp subsOp = DistributionListSubscribeOp.fromString(eSubsReq.getAttribute("op"));
/*     */       
/* 805 */       boolean bccOwners = eSubsReq.getAttributeBool("bccOwners", true);
/* 806 */       String memberEmail = eSubsReq.getText();
/*     */       
/* 808 */       Account memberAcct = this.prov.get(Key.AccountBy.name, memberEmail);
/* 809 */       if (memberAcct == null) {
/* 810 */         throw ServiceException.DEFEND_ACCOUNT_HARVEST(memberEmail);
/*     */       }
/* 812 */       boolean isMember = this.group.isMemberOf(memberAcct);
/*     */       
/* 814 */       boolean processed = false;
/* 815 */       if (isMember) {
/* 816 */         if (subsOp == DistributionListSubscribeOp.subscribe)
/*     */         {
/* 818 */           ZimbraLog.account.debug("AcceptSubsReqHandler: " + memberEmail + " is currently a member in list " + this.group.getName() + ", no action taken for the subscribe request");
/*     */         }
/*     */         else
/*     */         {
/* 822 */           DistributionListDocumentHandler.removeGroupMembers(this.prov, this.group, new String[] { memberEmail });
/* 823 */           processed = true;
/*     */         }
/*     */         
/*     */       }
/* 827 */       else if (subsOp == DistributionListSubscribeOp.subscribe) {
/* 828 */         DistributionListDocumentHandler.addGroupMembers(this.prov, this.group, new String[] { memberEmail });
/* 829 */         processed = true;
/*     */       }
/*     */       else {
/* 832 */         ZimbraLog.account.debug("AcceptSubsReqHandler: " + memberEmail + " is currently not a member in list " + this.group.getName() + ", no action taken for the un-subscribe request");
/*     */       }
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 838 */       if (processed) {
/* 839 */         ZimbraLog.security.info(ZimbraLog.encodeAttrs(new String[] { "cmd", "DistributionListAction", "op", getAction().name(), "name", this.group.getName(), "subsOp", subsOp.name(), "member", memberEmail }));
/*     */         
/*     */ 
/*     */ 
/*     */ 
/* 844 */         DistributionListAction.SubscriptionResponseSender notifSender = new DistributionListAction.SubscriptionResponseSender(this.prov, this.group, this.authedAcct, memberAcct, subsOp, bccOwners, true, null);
/*     */         
/*     */ 
/* 847 */         DistributionListAction.SubscriptionResponseSender.access$500(notifSender);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private static class RejectSubsReqHandler
/*     */     extends DistributionListAction.DLActionHandler
/*     */   {
/*     */     protected RejectSubsReqHandler(Element eAction, Group group, Provisioning prov, Account authedAcct)
/*     */     {
/* 857 */       super(group, prov, authedAcct);
/*     */     }
/*     */     
/*     */     DistributionListAction.Operation getAction()
/*     */     {
/* 862 */       return DistributionListAction.Operation.rejectSubsReq;
/*     */     }
/*     */     
/*     */     void handle()
/*     */       throws ServiceException
/*     */     {
/* 868 */       Element eSubsReq = this.eAction.getElement("subsReq");
/* 869 */       DistributionListSubscribeOp subsOp = DistributionListSubscribeOp.fromString(eSubsReq.getAttribute("op"));
/*     */       
/* 871 */       boolean bccOwners = eSubsReq.getAttributeBool("bccOwners", true);
/* 872 */       String memberEmail = eSubsReq.getText();
/*     */       
/* 874 */       Account memberAcct = this.prov.get(Key.AccountBy.name, memberEmail);
/* 875 */       if (memberAcct == null) {
/* 876 */         throw ServiceException.DEFEND_ACCOUNT_HARVEST(memberEmail);
/*     */       }
/* 878 */       boolean isMember = this.group.isMemberOf(memberAcct);
/*     */       
/* 880 */       boolean processed = false;
/* 881 */       if (isMember) {
/* 882 */         if (subsOp == DistributionListSubscribeOp.subscribe)
/*     */         {
/* 884 */           ZimbraLog.account.debug("RejectSubsReqHandler: " + memberEmail + " is currently a member in list " + this.group.getName() + ", no action taken for the subscribe request");
/*     */         }
/*     */         else
/*     */         {
/* 888 */           processed = true;
/*     */         }
/*     */         
/*     */       }
/* 892 */       else if (subsOp == DistributionListSubscribeOp.subscribe) {
/* 893 */         processed = true;
/*     */       }
/*     */       else {
/* 896 */         ZimbraLog.account.debug("RejectSubsReqHandler: " + memberEmail + " is currently not a member in list " + this.group.getName() + ", no action taken for the un-subscribe request");
/*     */       }
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 902 */       if (processed) {
/* 903 */         ZimbraLog.security.info(ZimbraLog.encodeAttrs(new String[] { "cmd", "DistributionListAction", "op", getAction().name(), "name", this.group.getName(), "subsOp", subsOp.name(), "member", memberEmail }));
/*     */         
/*     */ 
/*     */ 
/*     */ 
/* 908 */         DistributionListAction.SubscriptionResponseSender notifSender = new DistributionListAction.SubscriptionResponseSender(this.prov, this.group, this.authedAcct, memberAcct, subsOp, bccOwners, false, null);
/*     */         
/*     */ 
/* 911 */         DistributionListAction.SubscriptionResponseSender.access$500(notifSender);
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/account/DistributionListAction.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */